package gui;

import gui.processor.Processor;
import gui.utils.Para;
import gui.win.about.About;
import gui.win.barcode.BarCodes;
import gui.win.batch.Batch;
import gui.win.data.add.AddD;
import gui.win.proj.add.AddP;
import gui.win.data.rm.RmD;
import gui.win.genotree.Genotree;
import gui.win.genotype.Genotype;
import gui.win.proj.rm.RmP;
import gui.win.para.Alignment;
import gui.win.para.Kmers;
import gui.win.para.Misc;
import gui.win.para.Predator;
import gui.win.veme.Veme;
import java.awt.event.*;
import java.lang.Math.*;
import java.util.ArrayList;
import javax.swing.*;
import rpgsdb.db.NK;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Menu extends JMenuBar implements ActionListener{
    private JMenuItem addProject, deleteProject, addDataset, deleteDataset;
    private JMenuItem kmerMatching, palignment, miscellaneous, predator;
    private JMenuItem sortBarcodes, genotypeRegion, genotypeTree, vemeTable, addBatch;
    private JMenuItem about;
    private Segl1 segL1;
    public Menu(Segl1 segL1){
        this.segL1 = segL1;
        JMenu fileMenu = new JMenu("Project");
        addJMenuItem(addProject = new JMenuItem("Add Project"), fileMenu);
        addJMenuItem(deleteProject = new JMenuItem("Delete Project"), fileMenu);
        this.add(fileMenu);
        JMenu dataMenu = new JMenu("Data");
        addJMenuItem(addDataset = new JMenuItem("Add Dataset"), dataMenu);
        addJMenuItem(deleteDataset = new JMenuItem("Delete Dataset"), dataMenu);
        this.add(dataMenu);
        JMenu parametersMenu = new JMenu("Parameters");
        addJMenuItem(kmerMatching = new JMenuItem("Matching"), parametersMenu);
        addJMenuItem(palignment = new JMenuItem("Alignment"), parametersMenu);
        addJMenuItem(miscellaneous = new JMenuItem("Miscellaneous"), parametersMenu);
        addJMenuItem(predator = new JMenuItem("Predator"), parametersMenu);
        this.add(parametersMenu);
        JMenu tools = new JMenu("Tools");
        addJMenuItem(sortBarcodes = new JMenuItem("Barcodes"), tools);
        addJMenuItem(vemeTable = new JMenuItem("VEME Table"), tools);
        addJMenuItem(genotypeRegion = new JMenuItem("Genotype"), tools);
        addJMenuItem(genotypeTree = new JMenuItem("Genotree"), tools);
        //addJMenuItem(addBatch = new JMenuItem("Batch"), tools);
        this.add(tools);
        JMenu aboutMenu = new JMenu("About");
        addJMenuItem(about = new JMenuItem("About"), aboutMenu);
        this.add(aboutMenu);
    }

    private void addJMenuItem(JMenuItem menuItem, JMenu menuToAddTo){       
        menuItem.addActionListener(this);
        menuToAddTo.add(menuItem);
    }

    //also used for the treepopup - not here
    public static JFrame curJFrame;
    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource(); 
        try{
            if(source == sortBarcodes){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new BarCodes(segL1);
            }
            else if(source == genotypeRegion){
                if(Para.PB!=null){
                    if(curJFrame!=null){curJFrame.dispose();}
                    curJFrame = new Genotype(0, segL1);
                }
                else{JOptionPane.showMessageDialog(null, "No dataset loaded.", "alert", JOptionPane.ERROR_MESSAGE);}
            }
            else if(source == genotypeTree){
                if(Para.PB!=null){
                    if(curJFrame!=null){curJFrame.dispose();}
                    curJFrame = new Genotree(segL1);
                }
                else{JOptionPane.showMessageDialog(null, "No dataset loaded.", "alert", JOptionPane.ERROR_MESSAGE);}
            }
            else if(source == vemeTable){
                if(Para.PB!=null){
                    if(curJFrame!=null){curJFrame.dispose();}
                    curJFrame = new Veme(segL1);
                }
                else{JOptionPane.showMessageDialog(null, "No dataset loaded.", "alert", JOptionPane.ERROR_MESSAGE);}
            }
            else if(source == addBatch){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new Batch(segL1);
            }
            else if(source == addProject){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new AddP(segL1);
            }
            else if(source == deleteProject){
                ArrayList <NK> projects = Segl0.db.getProjNK();
                if(projects.size()>0){
                    if(curJFrame!=null){curJFrame.dispose();}
                    curJFrame = new RmP(segL1);
                }
                else{JOptionPane.showMessageDialog(null, "There are no projects to delete.", "alert", JOptionPane.ERROR_MESSAGE);}
            }
            else if(source == addDataset){
                ArrayList <NK> projects = Segl0.db.getProjNK();
                if(projects.size()>0){
                    if(curJFrame!=null){curJFrame.dispose();}
                    curJFrame = new AddD(segL1);
                }
                else{JOptionPane.showMessageDialog(null, "There are no projects to add data to. Please create a project first.", "alert", JOptionPane.ERROR_MESSAGE);}
            }
            else if(source == deleteDataset){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new RmD(segL1);
            }
            else if(source == miscellaneous){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new Misc(segL1);
            }
            else if(source == kmerMatching){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new Kmers(segL1);
            }
            else if(source == palignment){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new Alignment(segL1);
            }
            else if(source == about){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new About(segL1);
            }
            else if(source == predator){
                if(curJFrame!=null){curJFrame.dispose();}
                curJFrame = new Predator(segL1);
            }
        }
        catch(Exception e){e.printStackTrace();}
    }
}